/* indent size: 2 */

module.exports = app => {
  const DataTypes = app.Sequelize;

  const Users_coupons = app.model.define('users_coupons', {
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true
    },
    uid: {
      type: DataTypes.INTEGER,
      // allowNull: false,
      comment: '购物车所属用户id',
      references: {
        model: 'users',
        key: 'id'
      }
    },
    cid: {
      type: DataTypes.INTEGER,
      // allowNull: false,
      comment: '优惠券id',
      references: {
        model: 'coupons',
        key: 'id'
      }
    },
    state: {
      type: DataTypes.INTEGER,
      // allowNull: false,
      defaultValue: 0,
      comment: '优惠券状态【0->已领取；1->已使用；2->已过期】',
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: '创建时间'
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      comment: '更新时间'
    }
  }, {
    tableName: 'users_coupons'
  });

  Users_coupons.associate = function () {
    app.model.Coupons.belongsToMany(app.model.Users,
      {
        through: app.model.UsersCoupons,
        foreignKey: "cid",
        otherKey: "uid"
      })
      
    app.model.Users.belongsToMany(app.model.Coupons,
      {
        through: app.model.UsersCoupons,
        foreignKey: "uid",
        otherKey: "cid"
      })

  }

  return Users_coupons;
};
